import java.util.HashSet;

/**
* @description: 840. 矩阵中的幻方
* @author hewei
* @date 2022/9/7 15:24
* @version 1.0
*/

public class NumMagicSquaresInside {

    public int numMagicSquaresInside(int[][] grid) {
        int ans = 0;
        int m = grid.length;
        int n = grid[0].length;
        if (m < 3 || n < 3) return 0;
        for (int i = 0; i < m - 2; i++) {
            for (int j = 0; j < n - 2; j++) {
                HashSet<Integer> set = new HashSet<>();
                boolean flag = true;
                for (int k = i; k < i + 3; k++) {
                    if (!flag) break;
                    for (int l = j; l < j + 3; l++) {
                        if (grid[i][j] == 0 || grid[i][j] > 9 || set.contains(grid[i][j])) {
                            flag = false;
                            break;
                        }
                        set.add(grid[i][j]);
                    }
                }
            }
        }
        return 0;
    }
}